/**
 * This program is free software but WITHOUT ANY WARRANTY OR CONDITIONS OF ANY KIND.
 *
 * Copyright (C) 2008-2013 www.pretty-tools.com
 */

function makeMultiline()
{
    var elementTemplate = document.getElementById("element_template");
    if (!elementTemplate || elementTemplate.tagName == "TEXTAREA")
        return;

    var value = elementTemplate.value;
    var parentNode = elementTemplate.parentNode;
    parentNode.removeChild(elementTemplate);

    elementTemplate = document.createElement("TEXTAREA");
    elementTemplate.id = "element_template";
    elementTemplate.rows = 8;
    elementTemplate.style.width = "100%";
    elementTemplate.style.height = "100%";
    elementTemplate.value = value;
    window.dialogHeight = "350px";
    parentNode.appendChild(elementTemplate);
    parentNode.style.height = "100%";
}

function onPageLoad()
{
    yrprxHTMLContentHelper.enumerateElementsByTagName(document, "translate", function(elem) {
        if (elem && elem.innerText && elem.innerText.length > 0)
            elem.innerText = getMessage(elem.innerText);
    });
    yrprxHTMLContentHelper.enumerateElementsByTagName(document, "INPUT", function(elem) {
        if (elem && elem.type && elem.type == "button" && elem.value && elem.value.length > 0)
            elem.value = getMessage(elem.value);
        if (elem && elem.getAttribute("message_on_empty"))
            elem.setAttribute("message_on_empty", getMessage(elem.getAttribute("message_on_empty")))
    });
    document.body.setAttribute("edit_title", getMessage(document.body.getAttribute("edit_title")))

    onLoad();

    var selected_id = "tag_" + document.getElementById("element_tag").value;
    var selected_elem = document.getElementById(selected_id.toLowerCase());
    if (selected_elem) selected_elem.checked = true;

    var btCancel = document.getElementById("button-cancel");
    var btOK = document.getElementById("button-ok");
    if (btOK && btCancel && btCancel.clientWidth)
        btOK.style.width = btCancel.clientWidth + "px";

    btOK.onclick = onButtonOK;
    btCancel.onclick = function () {
        window.close();
    };
}

function onButtonOK()
{
    var radioGroup = document.getElementsByName("element_tag");
    for (var i = 0; i < radioGroup.length; ++i)
    {
        if (!radioGroup[i].checked)
            continue;

        var element_tag = document.getElementById("element_tag");
        element_tag.value = radioGroup[i].value;
        element_tag.selectedItem = radioGroup[i];
        break;
    }
    if (onOK())
        close();
}

window.arguments = window.dialogArguments;
getMessage = window.dialogArguments[1];
wsshResources = window.dialogArguments[2];
document.title = getMessage("webslideshow_rule_title_add");

addEventListener("load", onPageLoad);
